package com.fx.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fx.entity.UserAddress;
import com.fx.mapper.UserAddressMapper;
import com.fx.service.UserAddressService;
import com.fx.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public UserAddress getById(String flName) {
        return userAddressMapper.getById(flName);
    }

    @Override
    public List<UserAddress> getList(Map<String, Object> map) {
        return userAddressMapper.queryList(map);
    }

    @Override
    public int getCount(Map<String, Object> map) {
        return userAddressMapper.queryCount(map);
    }

    @Override
    public int save(UserAddress userAddress) {
        userAddress.setFlId(String.valueOf(idWorker.nextId()));
        userAddress.setFlCreatedTime(DateUtil.now());
        return userAddressMapper.save(userAddress);
    }

    @Override
    public int updateById(UserAddress userAddress) {
        return userAddressMapper.updateById(userAddress);
    }

    @Override
    public int remove(String id) {
        return userAddressMapper.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return userAddressMapper.batchRemove(ids);
    }

    @Override
    public List<UserAddress> getListByUserId(String userId) {
        return userAddressMapper.queryListByUserId(userId);
    }

    @Override
    public int changeDefaultAddress(String addressId) {
        UserAddress address = userAddressMapper.getById(addressId);
        String userId = address.getFlUserId();
        List<UserAddress> userAddressList = userAddressMapper.queryListByUserId(userId);
        for (UserAddress userAddress : userAddressList) {
            if(userAddress.getFlIsDefault() == 1) {
                userAddress.setFlIsDefault(0);
                userAddressMapper.updateById(userAddress);
            }
        }
        address.setFlIsDefault(1);
        return userAddressMapper.updateById(address);
    }

    @Override
    public UserAddress saveOrEdit(UserAddress userAddress) {
        String id = userAddress.getFlId();
        if(userAddress.getFlIsDefault() == 1){
            String userId = userAddress.getFlUserId();
            List<UserAddress> userAddressList = userAddressMapper.queryListByUserId(userId);
            for (UserAddress address : userAddressList) {
                if(address.getFlIsDefault() == 1) {
                    address.setFlIsDefault(0);
                    userAddressMapper.updateById(address);
                }
            }
        }
        if(id != null && !"".equals(id)){
            this.updateById(userAddress);
        }else{
            this.save(userAddress);
        }
        return userAddress;
    }

}
